package com.mobilesales.android.activities;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Spinner;

import com.mobilesales.android.c2dm.C2DMMessaging;
import com.mobilesales.android.c2dm.NotificationReceiver;

public abstract class ModelActivity<T> extends Activity implements IRefreshable {
	
	private T model;
	private NotificationReceiver receiver;
	
	
	protected void setModel(T model) {
		this.model = model;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		receiver = new NotificationReceiver() {
			@Override
			public void onMessage(String msg) {
				refresh();
			}
		};
		C2DMMessaging.addNotificationReceiver(receiver);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (receiver!=null)
			C2DMMessaging.removeNotificationReceiver(receiver);
	}

	protected T getModel() {
		return this.model;
	}

	protected boolean validate() {
		return true;
	}

	public void finish(View v) {
		finish();
	}
	
	public void fixSpinner(Spinner spinner) {
		 DisplayMetrics d = new DisplayMetrics(); 
		 getWindow().getWindowManager().getDefaultDisplay().getMetrics(d); 
		 int wdt=  d.widthPixels;
		 spinner.getLayoutParams().width=wdt-10;
	}
	private Handler handler = new Handler();
	
	protected Handler getHandler() {
		return this.handler;
	}	
	
}